import sys
from scrapy.spider import BaseSpider
from scrapy.selector import Selector 
from audiolib.items import AudiolibCategoryItem

class AudiolibCategorySpider(BaseSpider):
    name = "audiocat"
    allowed_domains = ["www.thereminvox.com"]
    start_urls = [
	"http://www.thereminvox.com/filemanager/list/1/index.html"
,
        "http://www.thereminvox.com/filemanager/list/2/index.html",
        "http://www.thereminvox.com/filemanager/list/3/index.html",
        "http://www.thereminvox.com/filemanager/list/4/index.html",
        "http://www.thereminvox.com/filemanager/list/5/index.html",
        "http://www.thereminvox.com/filemanager/list/6/index.html",
        "http://www.thereminvox.com/filemanager/list/7/index.html",
        "http://www.thereminvox.com/filemanager/list/8/index.html",
        "http://www.thereminvox.com/filemanager/list/9/index.html",
        "http://www.thereminvox.com/filemanager/list/11/index.html",
        "http://www.thereminvox.com/filemanager/list/12/index.html",
        "http://www.thereminvox.com/filemanager/list/16/index.html",
        "http://www.thereminvox.com/filemanager/list/17/index.html",
        "http://www.thereminvox.com/filemanager/list/18/index.html",
        "http://www.thereminvox.com/filemanager/list/19/index.html"
    ]

    def parse(self, response):
	print "PARSE\n"
        sel = Selector(response)
        titles = sel.xpath("//div[@class='topicPageTitle']/text()")
	items = []
	###### titolo
	item = AudiolibCategoryItem()
	item['title'] = titles[0].extract()
	items.append(item)

	###### abstract
        abstracts = sel.xpath("//div[@class='topicAbstract']/text()")
	if abstracts:
		items[0]['abstract'] = abstracts[0].extract()
	else:
		items[0]['abstract'] = ""

	###### legacy_id
	items[0]['legacy_id'] = response.url.split('/')[-2]

	##### parent category
	#labels = sel.xpath("//a[@class='path'][position()>1]/text()")
	#labels_list = []
	#for label in labels:
	#	labels_list.append(label.extract())
	#if len(labels_list) > 1:
	#	parent = labels_list[-2]
	#items[0]['parent'] = parent

	##### parent legacy id
	bc_link = sel.xpath("//a[@class='path']/@href")[-2]
	parent_legacy_id = bc_link.extract().split('/')[-2]
	items[0]['parent_legacy_id'] = parent_legacy_id



        return items
